package com.mofang.utils;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.widget.TextView;

import com.mofang.R;
import com.pactera.framework.exception.IException;
import com.pactera.framework.exception.ServerException;
import com.pactera.framework.exception.TimeoutException;

public class ViewHelper {
	private Context context;
	public TextView textView;

	public ViewHelper(Context context) {
		this.context = context;
	}

	/**
	 * Save the file name of SharedPreference session
	 * 
	 * @Title: showBTNDialog
	 * @param icon
	 * @param title
	 * @param msg
	 * @param btn1_content
	 * @param positiveListener
	 * @param onCancelListener
	 */
	public void showBTNDialog(String msg, String btn1_content,
			DialogInterface.OnClickListener positiveListener,
			DialogInterface.OnCancelListener onCancelListener) {
		if (context == null) {
			return;
		}
		try {

			new AlertDialog.Builder(context).setMessage(msg)
					.setPositiveButton(btn1_content, positiveListener)
					.setOnCancelListener(onCancelListener).show();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * Dialog box pop-up message (2 button)
	 * 
	 * @Title: showBTN2Dialog
	 * @param icon
	 * @param title
	 * @param msg
	 * @param btn1_content
	 * @param btn2_content
	 * @param positiveListener
	 * @param negativeListener
	 * @param onCancelListener
	 */
	public void showBTN2Dialog(String title, String msg, String btn1_content,
			String btn2_content,
			DialogInterface.OnClickListener positiveListener,
			DialogInterface.OnClickListener negativeListener,
			DialogInterface.OnCancelListener onCancelListener) {
		// setCanceledOnTouchOutside
		if (context == null) {
			return;
		}
		AlertDialog alertDialog = new AlertDialog.Builder(context)
				.setTitle(title).setMessage(msg)
				.setPositiveButton(btn1_content, positiveListener)
				.setNegativeButton(btn2_content, negativeListener)
				.setOnCancelListener(onCancelListener).create();
		alertDialog.setCanceledOnTouchOutside(false);
		alertDialog.show();
	}

	public void showBTN1Dialog(String msg, String btn1_content,
			DialogInterface.OnClickListener neutralListener) {
		if (context == null) {
			return;
		}
		try {
			new AlertDialog.Builder(context).setMessage(msg)
					.setNeutralButton(btn1_content, neutralListener).show();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void showBTNDialog(String msg) {
		if (context == null) {
			return;
		}
		try {

			new AlertDialog.Builder(context)
					.setMessage(msg)
					.setPositiveButton(
							context.getResources().getString(R.string.comm_ok),
							new DialogInterface.OnClickListener() {
								@Override
								public void onClick(DialogInterface dialog,
										int which) {
									dialog.dismiss();
								}
							})
					.setOnCancelListener(
							new DialogInterface.OnCancelListener() {
								@Override
								public void onCancel(DialogInterface dialog) {
									dialog.dismiss();
								}
							}).show();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void showBTN1Dialog(String msg, final Activity activity) {
		if (context == null) {
			return;
		}
		try {
			new AlertDialog.Builder(context)
					.setMessage(msg)
					.setPositiveButton(
							context.getResources().getString(R.string.comm_ok),
							new DialogInterface.OnClickListener() {
								@Override
								public void onClick(DialogInterface dialog,
										int which) {
									dialog.dismiss();
									activity.finish();
								}
							})
					.setOnCancelListener(
							new DialogInterface.OnCancelListener() {
								@Override
								public void onCancel(DialogInterface dialog) {
									dialog.dismiss();
									activity.finish();
								}
							}).show();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void showBTNDialog(String msg, String title) {
		if (context == null) {
			return;
		}
		try {
			new AlertDialog.Builder(context)
					.setTitle(title)
					.setMessage(msg)
					.setPositiveButton(
							context.getResources().getString(R.string.comm_ok),
							new DialogInterface.OnClickListener() {
								@Override
								public void onClick(DialogInterface dialog,
										int which) {
									dialog.dismiss();
								}
							})
					.setOnCancelListener(
							new DialogInterface.OnCancelListener() {
								@Override
								public void onCancel(DialogInterface dialog) {
									dialog.dismiss();
								}
							}).show();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void showErrorDialog(IException ie) {
		if (context == null) {
			return;
		}
		try {

			String msg = null;
			if (ie instanceof ServerException) {
				msg = ie.getMessage();
			} else if (ie instanceof TimeoutException) {
				msg = context.getString(R.string.comm_request_timeout);
			} else {
				msg = context.getString(R.string.comm_no_internet);
			}
			new AlertDialog.Builder(context)
					.setMessage(msg)
					.setPositiveButton(
							context.getResources()
									.getString(R.string.comm_back),
							new DialogInterface.OnClickListener() {
								@Override
								public void onClick(DialogInterface dialog,
										int which) {
									dialog.dismiss();
								}
							})
					.setOnCancelListener(
							new DialogInterface.OnCancelListener() {
								@Override
								public void onCancel(DialogInterface dialog) {
									dialog.dismiss();
								}
							}).show();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static Activity getRootContext(Activity activity) {
		while (activity.getParent() != null) {
			activity = activity.getParent();
		}
		return activity;
	}

	/**
	 * session超时异常
	 * 
	 * @param ie
	 * @param positiveListener
	 * @param onCancelListener
	 */
	public void showErrorDialog2(IException ie,
			DialogInterface.OnClickListener positiveListener,
			DialogInterface.OnClickListener onCancelListener) {
		if (context == null) {
			return;
		}
		try {
			String msg = null;
			String title = context.getString(R.string.comm_txt_abnormal);
			msg = ie.getMessage();
			AlertDialog dialog = new AlertDialog.Builder(context)
					.setTitle(title)
					.setMessage(msg)
					.setPositiveButton(
							context.getResources().getString(R.string.comm_ok),
							positiveListener).create();
			dialog.setCanceledOnTouchOutside(false);
			dialog.show();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public ProgressDialog getProgressDialog(String message) {
		ProgressDialog mProgressDialog = new ProgressDialog(context);
		mProgressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);// 设置风格为圆形进度条
		mProgressDialog.setCanceledOnTouchOutside(false);
		mProgressDialog.setMessage(message);
		mProgressDialog.setIndeterminate(false);// 设置进度条是否为不明确
		return mProgressDialog;
	}

}